package teleradiologia.web;

import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import teleradiologia.domain.Patient;
import teleradiologia.domain.Person;
import teleradiologia.service.DiagnosysManipulator;
import teleradiologia.service.OrderingManipulator;
import teleradiologia.service.PatientManipulator;
import teleradiologia.service.PersonManipulator;

@Controller
public class PatientController {
	private PersonManipulator personManipulator;
	private PatientManipulator patientManipulator;
	private OrderingManipulator orderingManipulator;
	private DiagnosysManipulator diagnosysManipulator;

	private static final Log log = LogFactory.getLog(PatientController.class);

	public DiagnosysManipulator getDiagnosysManipulator() {
		return diagnosysManipulator;
	}

	public void setDiagnosysManipulator(
			DiagnosysManipulator diagnosysManipulator) {
		this.diagnosysManipulator = diagnosysManipulator;
	}

	public OrderingManipulator getOrderingManipulator() {
		return orderingManipulator;
	}

	public void setOrderingManipulator(OrderingManipulator orderingManipulator) {
		this.orderingManipulator = orderingManipulator;
	}

	public PersonManipulator getPersonManipulator() {
		return personManipulator;
	}

	public void setPersonManipulator(PersonManipulator personManipulator) {
		this.personManipulator = personManipulator;
	}

	public PatientManipulator getPatientManipulator() {
		return patientManipulator;
	}

	public void setPatientManipulator(PatientManipulator patientManipulator) {
		this.patientManipulator = patientManipulator;
	}

	@RequestMapping("/patient/patientHome.htm")
	public ModelAndView handlePatientHome() {
		Person me = ((Person) SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal());

		me = personManipulator.getById(me.getId());
		Patient patient = patientManipulator.getByIdPerson(me.getId());

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("orderings", this.orderingManipulator
				.getAllByPatient(patient.getIdpatient()));

		ModelAndView result = new ModelAndView("patient/patientHome", "model",
				myModel);

		return result;
	}

	@RequestMapping("/patient/orderingDetails.htm")
	public ModelAndView handleOrderingDetails(
			@RequestParam("orderingDetailsId") long id) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("ordering", this.orderingManipulator.getById(id));

		ModelAndView result = new ModelAndView("patient/orderingDetails",
				"model", myModel);

		return result;
	}

	@RequestMapping("/patient/showDiagnosys.htm")
	public ModelAndView handleShowDiagnosys(
			@RequestParam("orderingId") long idOrdering) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("diagnosys", this.diagnosysManipulator
				.getByIdOrdering(idOrdering));

		ModelAndView result = new ModelAndView("patient/showDiagnosys",
				"model", myModel);

		return result;
	}

}
